package B刷题.A栈和队列;

import java.util.Stack;

/**
 * @author wei.zhao
 * @description: 剑指 Offer 30.包含min函数的栈
 * @date 2022年12月01日 21:35
 */
public class Code02_MinStack {

    /**
     * 使用辅助栈维护当前元素进栈后的最小元素。
     */
    Stack<Integer> dataStack;

    Stack<Integer> minStack;

    /**
     * initialize your data structure here.
     */
    public Code02_MinStack() {
        dataStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int x) {
        dataStack.add(x);
        if (minStack.isEmpty() || minStack.peek() >= x) {
            minStack.add(x);
        }
    }

    public void pop() {
        int pop = dataStack.pop();
        if (pop == minStack.peek()) {
            minStack.pop();
        }
    }

    public int top() {
        return dataStack.peek();
    }

    public int min() {
        return minStack.peek();
    }

}
